/*
 * @lc app=leetcode id=90 lang=cpp
 *
 * [90] Subsets II
 */
class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        vector<vector<int>> ret;
        int len = nums.size();
        if (len < 1) {
            ret.push_back(nums);
            return ret;
        }

        sort(nums.begin(), nums.end());

        vector<int> v;
        loop(nums, 0, v, ret);
        return ret;   
    }

    void loop(vector<int>& nums, int p, vector<int>& v, vector<vector<int>>& ret) {

        if (p == nums.size()) {
            ret.push_back(v);
            return ;
        }

        v.push_back(nums[p]);
        loop(nums, p+1, v, ret);

        v.pop_back();
        loop(nums, p+1, v, ret);
    }
};

